<?php
/*老师，模型*/
class Teacher_model extends CI_Model
{
    public $table = 'teacher';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 返回数据表总条目个数
     * @return [type] [description]
     */
    public function count_all()
    {
        return $this->db->count_all($this->table);
    }
    /**
     * 得到某个学校的教师的数量
     * @return [type] [description]
     */
    public function count_by_school_id($school_id)
    {
        $sql = 'SELECT count(*) as count FROM teacher WHERE school_id=?';
        $res = $this->db->query($sql, $school_id)->row_array();
        return $res['count'];
    }

    /**
     * 获得所有的条目
     * @return [type] [description]
     */
    public function get_all()
    {
        $res = $this->db->get($this->table)->result_array();
        return $res;
    }

    /**
     * 通过id返回条目
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function get_by_id($id)
    {
        return $this->db->get_where($this->table, array('id' => $id))->row_array();
    }
    /**
     * 通过school_id返回条目
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function get_by_school_id($school_id)
    {
        return $this->db->get_where($this->table, array('school_id' => $school_id))->result_array();
    }

    /**
     * 通过school_id返回待审核的条目
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function get_teachers_for_check_by_school_id($school_id)
    {
        return $this->db->get_where($this->table, array('school_id' => $school_id, 'pass' => 0))->result_array();
    }
    /**
     * 添加一个条目
     * @param [type] $item [description]
     */
    public function add($item)
    {
        return $this->db->insert($this->table, $item);
    }

    /**
     * 删除一个条目
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delete($id)
    {
        return $this->db->delete($this->table, array('id' => $id));
    }

    /**
     * 更新一个条目
     * @param  [type] $id   [description]
     * @param  [type] $item [description]
     * @return [type]       [description]
     */
    public function update($id, $item)
    {
        $this->db->where('id', $id);
        return $this->db->update($this->table, $item);
    }

    /**
     * 得到所有老师，包括专业信息
     * @return [type] [description]
     */
    public function get_all_teachers_by_school_id($school_id)
    {
        $sql = 'SELECT t.id,t.name,m.name as major_name,t.teacher_number FROM teacher as t,major as m WHERE t.major_id=m.id and t.school_id=?';
        return $this->db->query($sql, $school_id)->result_array();
    }

    /**
     * 删除一个老师
     * @param  [type] $teacher_id [description]
     * @return [type]             [description]
     */
    public function delete_cascade($teacher_id)
    {
        $teacher = $this->get_by_id($teacher_id);
        //先删除这个老师
        if ($this->delete($teacher_id)) {
            //删除这个user
            $this->load->model('user_model');
            if ($this->user_model->delete($teacher['user_id'])) {
                return true;
            }
        }
        return false;
    }

    /**
     * 通过教职工号得到老师
     * @param  [type] $teacher_number [description]
     * @return [type]                 [description]
     */
    public function get_by_teacher_number($teacher_number)
    {
        $sql = 'SELECT * FROM teacher WHERE teacher_number=?';
        return $this->db->query($sql, $teacher_number)->row_array();
    }

    /**
     * 通过user_id返回一个teacher
     * @param  [type] $user_id [description]
     * @return [type]          [description]
     */
    public function get_by_user_id($user_id)
    {
        return $this->db->get_where($this->table, array('user_id' => $user_id))->row_array();
    }


    /**
     * 通过id返回teacher信息，包括专业
     * @param  [type] $teacher_id [description]
     * @return [type]             [description]
     */
    public function get_teacher_by_id($teacher_id)
    {
        $sql = 'SELECT t.id,t.user_id,t.teacher_number,t.name,t.school_id,t.pass,s.name as school_name,m.name as major_name FROM teacher as t,major as m,school as s WHERE t.major_id=m.id and s.id=t.school_id and t.id=?';
        return $this->db->query($sql, $teacher_id)->row_array();
    }

    /**
     * 通过username返回一个teacher
     * @param  [type] $user_id [description]
     * @return [type]          [description]
     */
    public function get_by_username($username)
    {
        $sql = 'SELECT t.id,t.user_id,t.teacher_number,t.name,t.major_id FROM teacher as t,user as u WHERE t.user_id=u.id and u.username=?';
        return $this->db->query($sql, $username)->row_array();
    }

    /**
     * 得到这个老师教的课程的数目
     * @return [type] [description]
     */
    public function get_logic_class_count($teacher_id)
    {
        $sql = 'SELECT count(*) as count FROM logic_class WHERE teacher_id=?';
        $res = $this->db->query($sql, $teacher_id)->row_array();
        return $res['count'];
    }


    /**
     * 返回一个老师的学生个数
     * @param  [type] $teacher_id [description]
     * @return [type]             [description]
     */
    public function get_students_count($teacher_id)
    {
        $sql = 'SELECT COUNT(DISTINCT s.id) as count FROM student as s,major as m,student_in_logic_class as silc,logic_class as lc WHERE s.major_id=m.id and s.id=silc.student_id and lc.id=silc.logic_class_id and lc.teacher_id=?';
        $res = $this->db->query($sql, $teacher_id)->row_array();
        return $res['count'];
    }

    /**
     * 得到个人的组卷的个数
     * @param  [type] $teacher_id [description]
     * @return [type]             [description]
     */
    public function get_private_module_count($teacher_id)
    {
        $sql = 'SELECT count(*) as count FROM module WHERE teacher_id=?';
        $res = $this->db->query($sql, $teacher_id)->row_array();
        return $res['count'];
    }
    /**
     * 查看数据库中这个学校是否已经有这个工号
     * @param  [type] $teacher_number [description]
     * @return [type]                 [description]
     */
    public function teacher_number_exist($teacher_number, $school_id = 7)
    {
        $sql = 'SELECT count(*) as count FROM teacher WHERE teacher.teacher_number =? and teacher.school_id=?';
        $row_teacher = $this->db->query($sql, array($teacher_number, $school_id))->row_array();
        return ($row_teacher['count'] > 0);
    }

    /**
     * 根据Teacher_id查看专业
     */
    public function findMajorById($teacher_id)
    {
        $sql = 'SELECT 	m.`name`,m.id FROM teacher AS t,school AS s,major as m WHERE m.school_id = s.id AND s.id = t.school_id AND t.id = ?';
        return $this->db->query($sql, $teacher_id)->row_array();
    }
}
